#include <vector>
#include <iostream>
#include <unordered_map>

using namespace std;

class Solution {
public:
    // 时间复杂度 O(N)，N 为 nums 的长度
    int findShortestSubArray(vector<int>& nums) {
        unordered_map<int, vector<int>> m;
        for (int i = 0; i < nums.size(); i++) {
            if (m.count(nums[i])) {
                m[nums[i]][0]++;
                m[nums[i]][2] = i; 
            } else {
                m[nums[i]] = { 1, i, i };
            }
        }

        int maxNum = 0;
        int minLen = 0;

        for (auto &[_, vec] : m) {
            if (maxNum < vec[0]) {
                maxNum = vec[0];
                minLen = vec[2] - vec[1] + 1;
            } else if (maxNum == vec[0]) {
                minLen = min(minLen, vec[2] - vec[1] + 1);
            }
        }

        return minLen;
    }
};

int main() {
  vector<int> nums = { 1, 2, 2, 3, 1, 4, 2 };

  cout <<  Solution().findShortestSubArray(nums) << endl;

  return 0;
}
